#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MAX_NUM 1000000

int getRand(int num);
void mathexp(FILE *f);

int getRand(int num)
{
    if(num <= 0) return 0;

    return (rand() % num) + 1;    
}

void mathexp(FILE *f)
{
    int x, y, z;
    x = getRand(10);
    y = getRand(10 - x);
    z = getRand(2);
    if (z == 1)
        fprintf(f, "%d + %d = %-12c", x, y, ' ');
    else {
        if (y > x) {
            z = x;
            x = y;
            y = z;
        }
        fprintf(f, "%d - %d = %-12c", x, y, ' ');
    }
}

int main(int argc, char *argv[])
{
    if (argc < 2)
    {
        printf("ERROR! Useage: ./ex1 4 100\n");
        return 1;
    }
    int i, col, n;
    col = atoi(argv[1]);
    n = atoi(argv[2]);
    if(col <= 0 || n <= 0) return 1;
    FILE *f = fopen("output.txt", "w");
    srand(time(NULL));
    for (i = 0; i < n; i++)
    {
        if(i % col == 0)
            fprintf(f, "\r\n");
        mathexp(f);
    }
    fprintf(f, "\r\n");
    fclose(f);

    return 0;
}
